package com.hunan.api.catv.service.impl.tencent_cloud.tms;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.utils.GsonUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.Region;
import com.tencentcloudapi.tms.v20200713.TmsClient;
import com.tencentcloudapi.tms.v20200713.models.TextModerationRequest;
import com.tencentcloudapi.tms.v20200713.models.TextModerationResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Service
public class TmsService {

    public static final Logger LOGGER = LoggerFactory.getLogger(TmsService.class);

    @Autowired
    private CacheBiz cacheBiz;

    public TextModerationResponse textModeration(String textContent) {
        TextModerationResponse response = null;
        if (StringUtils.isNotEmpty(textContent)) {
            TextModerationRequest request = new TextModerationRequest();
            request.setContent(Base64.getEncoder().encodeToString(textContent.getBytes(StandardCharsets.UTF_8)));
            try {
                response = getTmsClient().TextModeration(request);
            } catch (TencentCloudSDKException e) {
                LOGGER.error("腾讯文本审核出错：", e);
            }
        }
        return response;
    }

    public static void main(String[] args) throws TencentCloudSDKException {

        /*String secretId = "AKIDKm0ymLnka9ZmmfOzUlthMm1GLAlARPDl";

        String secretKey = "yiOp0jGbrrkPtDIF3yoQwCDl47uRejZI";

        Credential credential = new Credential(secretId, secretKey);

        TmsClient tmsClient = new TmsClient(credential, Region.Guangzhou.getValue());

        TextModerationRequest request  = new TextModerationRequest();

        request.setContent(Base64.getEncoder().encodeToString("吸金屏".getBytes(StandardCharsets.UTF_8)));

        TextModerationResponse textModerationResponse = tmsClient.TextModeration(request);

       */


    }

    private TmsClient getTmsClient() {
        JsonObject tmsConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_tms_conf), JsonObject.class);
        String secretId = tmsConf.get(Constants.tencent_cloud_secret_id_name).getAsString();
        String secretKey = tmsConf.get(Constants.tencent_cloud_secret_key_name).getAsString();
        Credential credential = new Credential(secretId, secretKey);
        TmsClient tmsClient = new TmsClient(credential, Region.Guangzhou.getValue());
        return tmsClient;
    }

}
